package com.atguigu.blog.web;

import com.atguigu.blog.pojo.Article;
import com.atguigu.blog.pojo.AuthUser;
import com.atguigu.blog.pojo.VideoInfo;
import com.atguigu.blog.service.ArticleService;
import com.atguigu.blog.service.UserService;
import com.atguigu.blog.service.VideoInfoService;
import com.atguigu.blog.util.UserInfoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("")
public class UserController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserService userService;

    @Autowired
    private VideoInfoService videoInfoService;

    /**
     * 个人中心
     */
    @GetMapping("/ucenter")
    public String ucenterPage(Map<String, Object> map, Authentication authentication, HttpSession session) {
        if (authentication==null) {
            return "redirect:/login";
        }
        String username = UserInfoUtil.getUsername(authentication);
        QueryWrapper<AuthUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        List<Article> list = null;
        List<AuthUser> userList = null;
        if (StringUtils.isNotBlank(username)) {
            if ("admin".equals(username)) {
                list = articleService.findAll();// 文章列表
                userList = userService.list(queryWrapper);// 用户列表
                List<VideoInfo> videoInfoList = videoInfoService.list();// 视频列表
                map.put("userList", userList);
                map.put("videoInfoList", videoInfoList);
            } else {
                list = articleService.findByUsername(username);
                QueryWrapper<VideoInfo> createBy = new QueryWrapper<VideoInfo>().eq("create_by", username);
                List<VideoInfo> videoInfoList = videoInfoService.list(createBy);
                map.put("videoInfoList", videoInfoList);
            }
        }
        map.put("ucenterList", list);
        session.setAttribute("ucenterList", list);
        return "ucenter";
    }

    /**
     * 删除用户
     */
    @GetMapping("/delete/user/{id}")
    public String delete(@PathVariable("id") String id, Map<String, Object> map) {
        userService.removeById(id);
        return "redirect:/ucenter";
    }

    /**
     * 修改密码页
     * @return
     */
    @GetMapping("/updatePwdPage")
    public String updatePwdPage() {
        return "updatePwd";
    }

    /**
     * 修改密码
     */
    @PostMapping("/updatePwd")
    public String updatePwd(Authentication authentication, AuthUser authUser, Map<String, Object> map) {
        String username = UserInfoUtil.getUsername(authentication);
        QueryWrapper<AuthUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        AuthUser user = userService.getOne(queryWrapper);
        String id = user.getId();

        // 新密码
        String password = authUser.getPassword();
        if (StringUtils.isBlank(password)) {
            map.put("password", "新密码不能为空！");
            return "updatePwd";
        }
        // 对新密码加密
        String hashpwd = BCrypt.hashpw(password, BCrypt.gensalt());
        user.setPassword(hashpwd);

        authUser.setId(id);
        authUser.setPassword(hashpwd);
        userService.updateById(authUser);
        return "redirect:/ucenter";
    }

}
